/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZDocument;
/*     */ import com.zimbra.client.ZItem.Flag;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.Document;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Folder.FolderOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.store.Blob;
/*     */ import com.zimbra.cs.store.MailboxBlob;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.File;
/*     */ import java.util.List;
/*     */ import junit.framework.TestCase;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestDocument
/*     */   extends TestCase
/*     */ {
/*  47 */   private static final String NAME_PREFIX = TestDocument.class.getSimpleName();
/*     */   private static final String USER_NAME = "user1";
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  52 */     cleanUp();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testNote()
/*     */     throws Exception
/*     */   {
/*  61 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  62 */     String folderId = Integer.toString(16);
/*  63 */     ZDocument doc = TestUtil.createDocument(mbox, folderId, NAME_PREFIX + "-doc.txt", "text/plain", "doc".getBytes());
/*  64 */     ZDocument note = TestUtil.createDocument(mbox, folderId, NAME_PREFIX + "-note.txt", "text/plain", "note".getBytes(), true);
/*  65 */     String flags = Character.toString(ZItem.Flag.note.getFlagChar());
/*  66 */     mbox.updateItem(note.getId(), null, null, flags, null);
/*     */     
/*     */ 
/*  69 */     doc = mbox.getDocument(doc.getId());
/*  70 */     assertEquals(null, doc.getFlags());
/*  71 */     assertEquals(flags, note.getFlags());
/*     */     
/*     */ 
/*  74 */     List<String> ids = TestUtil.search(mbox, "in:briefcase tag:\\note", "document");
/*  75 */     assertEquals(1, ids.size());
/*  76 */     assertEquals(note.getId(), (String)ids.get(0));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMoveNote()
/*     */     throws Exception
/*     */   {
/*  85 */     String USER2_NAME = "user2";
/*  86 */     String filename = NAME_PREFIX + "-testMoveNote.txt";
/*  87 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress("user1"));
/*  88 */     Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER2_NAME));
/*     */     
/*     */ 
/*  91 */     ZMailbox zmbx = TestUtil.getZMailbox("user1");
/*  92 */     String folderId = Integer.toString(16);
/*  93 */     ZDocument note = TestUtil.createDocument(zmbx, folderId, filename, "text/plain", "note".getBytes(), true);
/*  94 */     String flags = Character.toString(ZItem.Flag.note.getFlagChar());
/*     */     
/*  96 */     assertEquals(flags, note.getFlags());
/*     */     
/*  98 */     Mailbox remoteMbox = MailboxManager.getInstance().getMailboxByAccount(acct2);
/*  99 */     Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct);
/*     */     
/*     */ 
/* 102 */     TestUtil.deleteTestData(USER2_NAME, NAME_PREFIX);
/*     */     
/*     */ 
/* 105 */     remoteMbox.revokeAccess(null, 16, acct.getId());
/* 106 */     mbox1.revokeAccess(null, 16, acct2.getId());
/*     */     
/*     */ 
/* 109 */     remoteMbox.grantAccess(null, 16, acct.getId(), (byte)1, (short)7, null);
/* 110 */     mbox1.grantAccess(null, 16, acct2.getId(), (byte)1, (short)7, null);
/*     */     
/*     */ 
/* 113 */     zmbx.moveItem(note.getId(), acct2.getId() + ":" + 16, null);
/* 114 */     ZMailbox remoteZmbx = TestUtil.getZMailbox(USER2_NAME);
/* 115 */     String idStr = (String)TestUtil.search(remoteZmbx, "in:briefcase " + filename, "document").get(0);
/* 116 */     Document doc = remoteMbox.getDocumentById(null, Integer.parseInt(idStr));
/*     */     
/*     */ 
/* 119 */     assertEquals(note.getFlags(), doc.getFlagString());
/*     */     
/*     */ 
/* 122 */     remoteZmbx.moveItem(String.valueOf(doc.getId()), acct.getId() + ":" + 16, null);
/*     */     
/*     */ 
/* 125 */     remoteMbox.revokeAccess(null, 16, acct.getId());
/* 126 */     mbox1.revokeAccess(null, 16, acct2.getId());
/* 127 */     idStr = (String)TestUtil.search(zmbx, "in:briefcase " + filename, "document").get(0);
/*     */     
/* 129 */     doc = mbox1.getDocumentById(null, Integer.parseInt(idStr));
/*     */     
/*     */ 
/* 132 */     assertEquals(note.getFlags(), doc.getFlagString());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testPurgeRevision()
/*     */     throws Exception
/*     */   {
/* 141 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 142 */     Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT);
/* 143 */     Folder folder = mbox.createFolder(null, "/" + NAME_PREFIX + " testPurgeRevisions", fopt);
/* 144 */     Document doc = mbox.createDocument(null, folder.getId(), "test1.txt", "text/plain", NAME_PREFIX, "testPurgeRevisions", new ByteArrayInputStream("One".getBytes()));
/* 145 */     int id = doc.getId();
/* 146 */     File file1 = doc.getBlob().getLocalBlob().getFile();
/* 147 */     assertTrue(file1.exists());
/*     */     
/* 149 */     doc = mbox.addDocumentRevision(null, id, NAME_PREFIX, "test1.txt", "testPurgeRevisions", new ByteArrayInputStream("Two".getBytes()));
/* 150 */     int version = doc.getVersion();
/* 151 */     File file2 = doc.getBlob().getLocalBlob().getFile();
/* 152 */     assertTrue(file2.exists());
/* 153 */     mbox.addDocumentRevision(null, id, NAME_PREFIX, "test1.txt", "testPurgeRevisions", new ByteArrayInputStream("Three".getBytes()));
/*     */     
/* 155 */     mbox.purgeRevision(null, id, version, false);
/* 156 */     assertTrue(file1.exists());
/* 157 */     assertFalse(file2.exists());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testPurgeRevisions()
/*     */     throws Exception
/*     */   {
/* 166 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 167 */     Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT);
/* 168 */     Folder folder = mbox.createFolder(null, "/" + NAME_PREFIX + " testPurgeRevisions", fopt);
/* 169 */     Document doc = mbox.createDocument(null, folder.getId(), "test2.txt", "text/plain", NAME_PREFIX, "testPurgeRevisions", new ByteArrayInputStream("One".getBytes()));
/* 170 */     int id = doc.getId();
/* 171 */     File file1 = doc.getBlob().getLocalBlob().getFile();
/* 172 */     assertTrue(file1.exists());
/*     */     
/* 174 */     doc = mbox.addDocumentRevision(null, id, NAME_PREFIX, "test2.txt", "testPurgeRevisions", new ByteArrayInputStream("Two".getBytes()));
/* 175 */     int version = doc.getVersion();
/* 176 */     File file2 = doc.getBlob().getLocalBlob().getFile();
/* 177 */     assertTrue(file2.exists());
/* 178 */     mbox.addDocumentRevision(null, id, NAME_PREFIX, "test2.txt", "testPurgeRevisions", new ByteArrayInputStream("Three".getBytes()));
/*     */     
/* 180 */     mbox.purgeRevision(null, id, version, true);
/* 181 */     assertFalse(file1.exists());
/* 182 */     assertFalse(file2.exists());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testContentType()
/*     */     throws Exception
/*     */   {
/* 191 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 192 */     String folderId = Integer.toString(16);
/* 193 */     ZDocument doc1 = TestUtil.createDocument(mbox, folderId, NAME_PREFIX + "-docOne.doc", "application/octet-stream", "doc1".getBytes());
/* 194 */     ZDocument doc2 = TestUtil.createDocument(mbox, folderId, NAME_PREFIX + "-docTwo.xls", "application/ms-tnef", "doc2".getBytes());
/*     */     
/*     */ 
/* 197 */     assertEquals("application/msword", doc1.getContentType());
/* 198 */     assertEquals("application/vnd.ms-excel", doc2.getContentType());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testPublicShare()
/*     */     throws Exception
/*     */   {
/* 207 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 208 */     String folderName = NAME_PREFIX + "testPublicSharing";
/* 209 */     Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT);
/* 210 */     Folder folder = mbox.createFolder(null, "/" + folderName, fopt);
/* 211 */     Document doc = mbox.createDocument(null, folder.getId(), "test2.txt", "text/plain", NAME_PREFIX, "testPublicSharing", new ByteArrayInputStream("A Test String".getBytes()));
/* 212 */     mbox.grantAccess(null, folder.getId(), null, (byte)6, ACL.stringToRights("rw"), null);
/*     */     
/* 214 */     String URL = TestUtil.getBaseUrl() + "/home/" + mbox.getAccount().getName() + "/" + folderName;
/* 215 */     HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 216 */     GetMethod get = new GetMethod(URL);
/* 217 */     int statusCode = HttpClientUtil.executeMethod(eve, get);
/* 218 */     assertEquals("This request should succeed. Getting status code " + statusCode, 200, statusCode);
/* 219 */     String respStr = get.getResponseBodyAsString();
/* 220 */     assertFalse("Should not contain AUTH_EXPIRED", respStr.contains("AUTH_EXPIRED"));
/* 221 */     assertTrue("Should contain shared content ", respStr.contains("test2.txt"));
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception {
/* 225 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 230 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 235 */     TestUtil.cliSetup();
/* 236 */     TestUtil.runTest(TestDocument.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestDocument.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */